package cn.gzjp.modules.woms.service;

import java.util.Date;
import java.util.List;

import cn.gzjp.common.constant.Constants;
import cn.gzjp.modules.sys.utils.DictUtils;
import cn.gzjp.modules.woms.entity.EventPic;
import cn.gzjp.modules.woms.entity.EventProcess;
import cn.gzjp.modules.woms.entity.Worker;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.service.CrudService;
import cn.gzjp.modules.woms.entity.Event;
import cn.gzjp.modules.woms.dao.EventDao;

/**
 * 工单Service
 * @author liyp
 * @version 2018-07-07
 */
@Service
@Transactional(readOnly = true)
public class EventService extends CrudService<EventDao, Event> {
	@Autowired
	private EventProcessService eventProcessService;
	@Autowired
	private WorkerService workerService;
	@Autowired
	private EventPicService eventPicService;
	@Override
	public Event get(String id) {
		Event event = super.get(id);
		if(event!=null){
			event.setProcessList(this.findProcessList(id));
		}
		return event;
	}
	private List<EventProcess> findProcessList(String eventId){
		EventProcess search = new EventProcess();
		search.setEventId(eventId);
		List<EventProcess> processList = eventProcessService.findList(search);
		List<EventProcess> resultList = Lists.newArrayList();
		for(EventProcess process:processList){
			resultList.add(eventProcessService.get(process.getId()));
		}
		return resultList;
	}

	@Override
	public List<Event> findList(Event event) {
		return super.findList(event);
	}
	@Override
	public Page<Event> findPage(Page<Event> page, Event event) {
		return super.findPage(page, event);
	}
	@Override
	@Transactional(readOnly = false)
	public void save(Event event) {
		super.save(event);
	}

	@Transactional(readOnly = false)
	public void createEvent(Event event){
		this.save(event,event.getEventId());
		EventProcess process = new EventProcess();
		process.setEventId(event.getId());
		process.setWorker(new Worker(event.getCreaterId()));
		process.setStatus(event.getStatus());
		process.setContent("创建该工单");
		eventProcessService.save(process);
	}


	@Transactional(readOnly = false)
	public void save(Event event, String id) {
		super.insert(event,id);
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(Event event) {
		super.deleteByLogic(event);
	}


	/**
	 * 分派员分派工单
	 * @param event
	 * @param userId 当前操作人，分派员id
	 * @param engineerArr 工程师id 可能多个
	 * @return
	 */
	@Transactional(readOnly = false)
	public String assign(Event event,String userId,String[] engineerArr){
		Worker worker = workerService.get(userId);
		event.setStatus(Constants.EventStatus.assign);//变更为派单状态
		event.setAssignTime(new Date());
		event.setAssigner(worker);
		event.setEngineerIds("");
		String content="将该工单分派给外场运维人员  ";
		for (int i = 0; i < engineerArr.length; i++) {
			event.setEngineerIds(event.getEngineerIds() + engineerArr[i] + "|");
			Worker enginner = workerService.get(engineerArr[i]);
			content += enginner.getName()+"、";
		}
		content = content.substring(0,content.length()-1);
		this.save(event);
		EventProcess process = new EventProcess();
		process.setEventId(event.getId());
		process.setWorker(worker);
		process.setStatus(event.getStatus());
		process.setContent(content);
		eventProcessService.save(process);
		return content;
	}


	/**
	 * 工程师接收工单
	 * @param event
	 * @param userId
	 * @param zrgsPics
	 * @param faultPics
	 * @param lwpqPics
	 * @return
	 */
	@Transactional(readOnly = false)
	public Event receive(Event event,String userId, String[] zrgsPics,
						 String[] faultPics,
						 String[] lwpqPics){
		Worker worker = workerService.get(userId);
		this.save(event);
		//保存处置明细
		EventProcess process = new EventProcess();
		process.setWorker(worker);
		process.setEventId(event.getId());
		process.setStatus(Constants.EventStatus.receive);
		process.setContent(workerService.get(userId).getName() + "确认故障为" + DictUtils.getDictLabel(event.getZrgsType(),"zrgs_type","") +"<br/>"
				+"故障类型："+ DictUtils.getDictLabel(event.getFaultType(),"fault_type","")+"<br/>"
				+"外包置换："+ DictUtils.getDictLabel(event.getLwpqType(),"lwpq_type","")+"<br/>"
		);//责任归属描述
		eventProcessService.save(process);
		if(zrgsPics!=null&&zrgsPics.length>0){
			for(int i = 0; i < zrgsPics.length; i++){
				EventPic eventPic = new EventPic();
				eventPic.setEventId(event.getId());
				eventPic.setPic(zrgsPics[i]);
				eventPic.setType(Constants.PicType.gzhs);
				eventPic.setProcessId(process.getId());
				eventPicService.save(eventPic);
			}
		}if(faultPics!=null&&faultPics.length>0){
			for(int i = 0; i < faultPics.length; i++){
				EventPic eventPic = new EventPic();
				eventPic.setEventId(event.getId());
				eventPic.setPic(faultPics[i]);
				eventPic.setType(Constants.PicType.gzhs);
				eventPic.setProcessId(process.getId());
				eventPicService.save(eventPic);
			}
		}
		if(lwpqPics!=null&&lwpqPics.length>0){
			for(int i = 0; i < lwpqPics.length; i++){
				EventPic eventPic = new EventPic();
				eventPic.setEventId(event.getId());
				eventPic.setPic(lwpqPics[i]);
				eventPic.setType(Constants.PicType.gzhs);
				eventPic.setProcessId(process.getId());
				eventPicService.save(eventPic);
			}
		}
		return event;
	}
	@Transactional(readOnly = false)
	public Event process(Event event,String userId,String content,String[] pics) {
		this.save(event);
		EventProcess process = new EventProcess();
		process.setEventId(event.getId());
		process.setWorker(new Worker(userId));
		process.setContent(content);
		process.setStatus(event.getStatus());
		eventProcessService.save(process);
		EventPic eventPic = new EventPic();
		if (pics != null&&pics.length>0) {
			for (int i = 0; i < pics.length; i++) {
				eventPic.setEventId(event.getId());
				eventPic.setPic(pics[i]);
				eventPic.setType(Constants.PicType.gzhf);
				eventPic.setProcessId(process.getId());
				eventPicService.save(eventPic);
			}
		}
		return  event;
	}

	@Transactional(readOnly = false)
	public Event achieve(Event event,String userId,String content,String[] pics)  {
		event.setAchieve(Constants.EventAchieveStatus.YES);
		this.save(event);
		EventProcess process = new EventProcess();
		process.setEventId(event.getId());
		process.setWorker(new Worker(userId));
		process.setContent(content);
		process.setStatus(event.getStatus());
		eventProcessService.save(process);
		EventPic eventPic = new EventPic();
		if (pics != null&&pics.length>0) {
			for (int i = 0; i < pics.length; i++) {
				eventPic.setEventId(event.getId());
				eventPic.setPic(pics[i]);
				eventPic.setType(Constants.PicType.achieve);
				eventPic.setProcessId(process.getId());
				eventPicService.save(eventPic);
			}
		}
		return  event;
	}

}